import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store/index'
// eslint-disable-next-line no-unused-vars
import global from './components/global'
import axios from 'axios'
import {
  Message
} from 'element-ui'
import VueCookies from 'vue-cookies'
import VueLazyLoad from 'vue-lazyload'
import storage from './storage/index'
// import env from './env'
// 导入Nprogress包对应的js和css文件
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import './plugins/element.js'

Vue.use(VueCookies)
Vue.use(VueLazyLoad, {
  loading: require('./assets/loading-svg/loading-spokes.svg')
})
// 把storage挂载到vue原型上
Vue.prototype.storage = storage
// 把axios挂载到vue原型上
Vue.prototype.$axios = axios
// 请求基路径
axios.defaults.baseURL = '/api'
// axios.defaults.baseURL = env.baseURL
// 设置超时时间
axios.defaults.timeout = 8000
// 请求拦截
axios.interceptors.request.use(config => {
  // 展示进度条
  Nprogress.start()
  return config
})
// 响应拦截
axios.interceptors.response.use(response => {
  // 隐藏进度条
  Nprogress.done()
  const res = response.data
  if (res.status === 0) {
    return res
  } else if (res.status === 10) {
    // const hash = location.hash
    // if (hash.includes('index')) return
    Message.warning('您未登录，请先登陆！')
    // 未登录跳转到登陆页面
    window.location.href = '/#/login'
  } else {
    Message.warning(res.msg)
    return Promise.reject(res)
  }
}, (error) => {
  const res = error.response
  Message.error(res.data.message)
  return Promise.reject(error)
})
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
